<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="海报标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入海报标题"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['creative:poster:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['creative:poster::export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="缩略图" align="center" prop="thumb_url">
        <template #default="scope">
          <img
            :src="scope.row.thumb_url"
            style="width: 50px; display: inline-block; cursor: pointer"
            @click="previewImg(scope.row.thumb_url)"
          />
        </template>
      </el-table-column>
      <el-table-column label="高清图" align="center" prop="big_url">
        <template #default="scope">
          <img
            :src="scope.row.big_url"
            style="width: 50px; display: inline-block; cursor: pointer"
            @click="previewImg(scope.row.big_url)"
          />
        </template>
      </el-table-column>
      <el-table-column label="是否可见" align="center" prop="visible" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            v-hasPermi="['creative:poster:visible']"
            @click="handleVisible(scope.row)"
          >
            可见
          </el-button>
          <el-button
            link
            type="danger"
            v-hasPermi="['creative:poster:delete']"
            @click="handleDelete(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <!-- <PosterForm ref="formRef" @success="getList" /> -->

  <!-- 图片预览弹窗 -->
  <el-dialog v-model="dialogVisible" title="图片预览" width="30%">
    <img :src="dialogImageUrl" style="width: 100%" />
  </el-dialog>
</template>

<script setup lang="ts" name="EnrollClue">
import { dateFormatter } from '@/utils/formatTime'
import * as PosterApi from '@/mock/official-website/poster/index.ts'
// import PosterForm from './PosterForm.vue'
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const dialogVisible = ref(false) // 图片预览弹窗
const dialogImageUrl = ref('') // 图片预览弹窗的图片地址
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  title: null,
  thumb_url: null,
  big_url: null,
  visible: null,
  createTime: null
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PosterApi.getPosterPage(queryParams)
    // 数据处理
    data.list.forEach((item: any) => {
      item.visible = item.visible ? '可见' : '不可见'
    })
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await PosterApi.deletePoster(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 可见按钮操作 */
const handleVisible = async (obj) => {
  const data = { ...obj }
  data.visible = data.visible == 0 ? 1 : 0
  try {
    // 更新的二次确认
    await message.confirm('是否确定更改海报可见状态？')
    // 发起更新
    await PosterApi.updatePoster(data)
    message.success(t('common.updateSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const previewImg = (url) => {
  dialogImageUrl.value = url
  dialogVisible.value = true
}

/** 导出按钮操作 */
const handleExport = async () => {
  // try {
  //   // 导出的二次确认
  //   await message.exportConfirm()
  //   // 发起导出
  //   exportLoading.value = true
  //   const data = await PosterApi.exportEnrollClue(queryParams)
  //   download.excel(data, '客户报名线索.xls')
  // } catch {
  // } finally {
  //   exportLoading.value = false
  // }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
